﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using BusinessLayer;

namespace BoletoElectronicoDesktop.Login
{
    public partial class FormLogin : Form
    {

        public FormLogin(Common.Util.Funcionalidad Use)
        {
            InitializeComponent();
            lblErr.Visible = false;
            FormClosed += new FormClosedEventHandler(FormLogin_FormClosed);
        }

        public bool Logged { get; set; }

        private void FormLogin_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (!Logged)
            {
                throw new ApplicationException("Loginearse es obligatorio para usar esta funcionalidad.");
            }
            else
            {
                if (txtUser.Text == txtPass.Text)
                {
                    FormConfirmNewPass frm;

                    frm = new FormConfirmNewPass(Util.Funcionalidad.Login);
                    frm.ShowDialog(this);

                }
            }
        }

        private void btnIngresar_Click(object sender, EventArgs e)
        {
            bool error = false;
            lblErr.Visible = error;

            try
            {
                Logged = BizAdmin.LogIn(txtUser.Text, txtPass.Text);
                lblErr.Visible = !Logged;
            }
            catch (ApplicationException appEx)
            {
                error = true;
                lblErr.Text = appEx.Message;
                lblErr.Visible = error;
            }

            if (!error)
            {
                this.Close();
            }
        }


    }
}
